package com.iptv2.system.web.listener;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.iptv2.common.cache.RedisSessionUtil;
import com.iptv2.common.spring.SpringUtil;
import com.iptv2.system.service.OnlineUserService;

public class SessionListener implements  HttpSessionListener {
	private static final String ONLINEUSER_MAP_KEY = "online_users";
	private OnlineUserService onlineUserService;
	RedisSessionUtil redisSessionUtil;
	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		onlineUserService =SpringUtil.getBean(OnlineUserService.class);
		redisSessionUtil =SpringUtil.getBean(RedisSessionUtil.class);
		String sessionid =arg0.getSession().getId();
		if(redisSessionUtil.hasKey(ONLINEUSER_MAP_KEY,sessionid)){
			onlineUserService.delete(sessionid);
		}
	}

}
